
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Security;

namespace eCom.Gui
{
	public class SalesOrderPaymentWizardBizProc : BaseBusinessProcess
	{
		public SalesOrderPaymentWizardBizProc(ActiveBizProc procState)
			: base(procState)
		{
		}

		protected override void setupRuleNames()
		{
			//m_getBizObjDetailsRule = "getCheckOutWizard";
			m_getBizObjListRule = "getUserPaymentTerms";
			m_updateBizObjRule = "updateSalesOrderPaymentTermInfo";
		}

		protected override object[] getBizObjListRuleParameters(object parentId, IBizObj filterSort)
		{
			return new object[] { CurrentUser.get("id"), m_procState.get("orderInfo") };
		}

		protected override bool shouldCallGetBizObjListRule(object parentBizObjId)
		{
			return !m_hadValidationErrors;
		}

		public override Privileges CurrentEditPrivilege
		{
			get { return Privileges.Edit; }
		}

		protected override object getCurrentBizObjId()
		{
			object id = null;
			IBizObj order = m_procState.get("orderInfo") as IBizObj;
			if (order != null)
				id = order.get("paymentMethod.id");

			if(id == null)
				id = base.getCurrentBizObjId();

			return id;
		}

		public override IResults updateBizObj(IBizObj obj)
		{
			IResults rc = null;
			List<ValidationError> errors = obj.validate();
			if (errors == null || errors.Count == 0)
			{
				rc = base.updateBizObj(obj);
				IBizObj paymentControl = rc.CallResults as IBizObj;
				if (paymentControl != null && !Cmn.IsEmpty(paymentControl.get("redirectUrl")))
				{
				}
				if (rc.ExceptionInfo == null && paymentControl != null && Cmn.IsTrue(paymentControl.get("paymentSuccessful")))
				{
					IBizObj wizardInfo = m_procState.get("info") as IBizObj;
					if (wizardInfo != null)
						wizardInfo.put("stepCompleted", true);
				}
				else if (paymentControl != null)
				{
					if (!Cmn.IsEmpty(paymentControl.get("errorMessageId")))
						m_procState.BPC.Messenger.add((string)paymentControl.get("errorMessageId"), (Swaf.Container.FlexiMap)paymentControl.get("errorInfo"));
					else
						m_procState.BPC.Messenger.add("Error-GeneralPaymentFailure", new Swaf.Container.FlexiMap());
				}
			}
			else
			{
				m_hadValidationErrors = true;
				foreach (ValidationError e in errors)
					m_procState.BPC.Messenger.add("Error-InvalidParameter", e);
			}
			return rc;
		}

		protected override object[] getUpdateBizRuleParameters(IBizObj bizObj)
		{
			string viewName = m_procState.BPC.HostView.ViewName;
			IBizObj order = m_procState.get("orderInfo") as IBizObj;
			return new object[] { viewName, order, bizObj };
		}

		public void setupOrderPaymentType()
		{
			showData(ForceLevel.mustShowForceDataRetrieval);
			IBizObj order = m_procState.get("orderInfo") as IBizObj;
			if (order != null && CurrentBizObj != null)
			{
				IBizObj term = App.bizObjMgr.create((string)CurrentBizObj.get("bizObjType"));
				term.put("id", CurrentBizObj.get("id"));
				order.put("paymentMethod", term);
			}
		}

		protected override void getBizObjList()
		{
			base.getBizObjList();
			base.setChildTypeVisibility(BizProcWidgetType.list, !(m_bizobjs == null || m_bizobjs.numElements == 0));
		}

		protected override void setupActions()
		{
			base.setupActions();
			Actions.addAction(new GenericAction("SetupPaymentType", true, setupOrderPaymentType, this));
		}

	}
}
